<?php if(!defined('MS_XTIGER')) exit('Access Denied');
/**
 * Created by openXtiger.org.
 * User: xtiger
 * Date: 2009-6-10
 * Time: 21:21:51
 */

global $common; 
$cachemodel = xt_load('model','cms.cachemodel');
$common  = $cachemodel('getCommon');
define('CACHE_MODEL_PATH',MS_CACHEPATH.'model/');
define('MS_FIELDS_PATH', MS_APPPATH.'fields/');

/*public */function xtb_cms_addcategory($pid = 0) {
    global $common;
    $cm = xt_load('model','cms.categorymodel');
    if (xt_pget('submit')) {
        $category = xt_pget('category');
        if(empty($category['upload_maxsize'])) $category['upload_maxsize'] = 0;
        if(empty($category['thumb_width'])) $category['thumb_width'] = 0;
        if(empty($category['thumb_height'])) $category['thumb_height'] = 0;
        $r = $cm('add',$category);
        if($r > 0) {
            return xtr_message('add_category_success',"/cms/editcategory/$category[parentid]/$r");
        }
        return xtr_message('add_category_fail');
    }
    xt_set('cList',$cm('select',$common['CATEGORY'],0, 'category[parentid]', 'parentid', '无（作为一级分类）', $pid,'',2));
    xt_set('models',$common['MODEL']);
    return 'cms/addcategory';
}
/*public */function xtb_cms_editcategory($pid = 0,$cid = 0) {
    global $common;
    $cm = xt_load('model','cms.categorymodel');
    if (xt_pget('submit')) {
        $category = xt_pget('category');
        if(empty($category['upload_maxsize'])) $category['upload_maxsize'] = 0;
        if(empty($category['thumb_width'])) $category['thumb_width'] = 0;
        if(empty($category['thumb_height'])) $category['thumb_height'] = 0;
        $r = $cm('edit', $cid, $category, $pid);
        if($r > 0) {
            return xtr_message('edit_category_success',"/cms/editcategory/$pid/$cid");
        }
        return xtr_message('edit_category_fail');
    }
    xt_set('cList',$cm('select',$common['CATEGORY'],0, 'category[parentid]', 'parentid', '无（作为一级分类）', $pid,'',2));
    $category = $cm('get',$cid);
    xt_set('category',$category);
    xt_set('pid',$pid);
    xt_set('modelname',$common['MODEL'][$category['modelid']]['name']);
    return 'cms/editcategory';
}
/*public */function xtb_cms_category($pid = -1) {
    global $common;
    $cm = xt_load('model','cms.categorymodel');
    xt_set('data',$cm('listinfo',$pid));
    xt_set('MODEL',$common['MODEL']);
    return 'cms/category';
}
/*public */function xtb_cms_addsite($pid = 0) {
    global $common;
    $type = xt_get('type');
    if (xt_pget('submit')) {
        $sm = xt_load('model','cms.sitemodel');
        $site = xt_pget('site');
        $setting = xt_pget('setting');
        $r = $sm('add',$site,$setting);
        if($r > 0) {
            return xtr_message('add_site_success',"/cms/editsite/$site[parentid]/$r");
        }
        return xtr_message('add_site_fail');
    }
    xt_set('type',$type);
    if($type=='') {
        $cm = xt_load('model','cms.sitemodel');
        xt_set('cList',$cm('select',$common['SITE'],0, 'parentid', 'parentid', $pid,'','',2));
        return 'cms/addsite';
    }
    $parentid = xt_pget('parentid');
    xt_set('parentid',$parentid);
    xt_set('catname',$parentid ? $common['SITE'][$parentid]['sitename'] : '无');
    return 'cms/addsite'.$type;   
}
/*public */function xtb_cms_editsite($pid = 0,$siteid = 0) {
    global $common;
    if (xt_pget('submit')) {
        $sm = xt_load('model','cms.sitemodel');
        $site = xt_pget('site');
        $setting = xt_pget('setting');
        $r = $sm('edit',$siteid, $site, $setting, $pid);
        if($r > 0) {
            return xtr_message('edit_site_success',"/cms/editsite/$site[parentid]/$siteid");
        }
        return xtr_message('edit_site_fail');
    }
    $sm = xt_load('model','cms.sitemodel');
    xt_set('cList',$sm('select',$common['SITE'],0, 'site[parentid]', 'parentid', $pid,'',2));
    $site = $sm('get',$siteid);
    if(!$site) {
        return xtr_message('edit_site_fail');
    }
    xt_set('site',$site);
    xt_set('setting',$site['setting']);
    xt_set('pid',$pid);

    return 'cms/editsite'.$site['type'];
}
/*public */function xtb_cms_site($pid=-1) {
    $cm = xt_load('model','cms.sitemodel');
    xt_set('data',$cm('listinfo',$pid));
    $cachemodel = xt_load('model','cms.cachemodel');
    $common  = $cachemodel('getCommon');
    return 'cms/site';
}
/*public */function xtb_cms_content($catid=0,$folder=0) {
    global $common;
    xt_set('catid',$catid);
    xt_set('folder',$folder);
    
    $categorys  = $common['CATEGORY'];
    $catpath= '';
    if(isset($categorys[$catid])){
        foreach(explode(',',$categorys[$catid]['parentids']) as $id){
            if($id==0) continue;
            $catpath .='&nbsp;&raquo;&nbsp;'.$categorys[$id]['catname'];
        }
        xt_set('catpath',$catpath.'&nbsp;&raquo;&nbsp;'.$categorys[$catid]['catname']);
    }

    xt_set('categorys',$categorys);
    
    $m = xt_load('model','cms.contentmodel');
    $perpage = intval(xt_gget('perpage',20));
    $page = intval(xt_gget('page',1));
    $pageSpport = $m('select',$catid, $folder, $page, $perpage, xt_gget('field'),xt_gget('q'));
    xt_set('data',xt_hashmap($pageSpport,'data'));

    xt_set('pagectrl',xt_hashmap($pageSpport,'pagectrl'));
    xt_set('pageurl',$pageSpport['pageurl'].'&perpage='.$perpage);
    return 'cms/content';
}
/*public */function xtb_cms_addpublish($siteid=0) {
    global $common;
    if (xt_pget('submit')) {
        $info = xt_pget('info',array());
        $rm = xt_load('model','cms.releasedmodel');
        $r = $rm('add',$info);
        if( $r > 0 ) {
            return xtr_message('add_publish_success',"/cms/released/$info[siteid]"); 
        }
        return xtr_message('add_publish_fail');
    }
    $catid = xt_gget('catid',0);
    xt_set('catid',$catid);
    xt_set('siteid',$siteid);
    $sites  = $common['SITE'];
    $sitepath= '';
    if(isset($sites[$siteid])) {
        foreach(explode(',',$sites[$siteid]['parentids']) as $id){
            if($id==0) continue;
            $sitepath .='&nbsp;&raquo;&nbsp;'.$sites[$id]['sitename'];
        }
        xt_set('sitepath',$sitepath.'&nbsp;&raquo;&nbsp;'.$sites[$siteid]['sitename']);
    }

    xt_set('sites',$sites);
    xt_set('categorys',$common['CATEGORY']);

    $m = xt_load('model','cms.contentmodel');
    $perpage = intval(xt_gget('perpage',20));
    $page = intval(xt_gget('page',1));
    $pageSpport = $m('select',$catid, 0, $page, $perpage, xt_gget('field'),xt_gget('q'));
    xt_set('data',xt_hashmap($pageSpport,'data'));

    xt_set('pagectrl',xt_hashmap($pageSpport,'pagectrl'));
    xt_set('pageurl',$pageSpport['pageurl'].'&perpage='.$perpage);
    $cm = xt_load('model','cms.categorymodel');
    xt_set('cList',$cm('select',$common['CATEGORY'],0, 'catid', 'catid', '', $catid,'',2));
    return 'cms/chcontent';
}
/*public */function xtb_cms_addcontent($catid = 0) {
    global $common;

    $categorys  = $common['CATEGORY'];
    if(!isset($categorys[$catid])){
        return xtr_message('add_content_fail');    
    }
    if (xt_pget('submit')) {
        $info = xt_pget('info',array());
        $publish = xt_pget('publish',array());
        if($publish['siteid']>0) {
            $info['folder'] = 0;    
        }
        if(isset($info['inputtime'])) $info['updatetime'] = $info['inputtime'];
        $m = xt_load('model','cms.contentmodel');
        $info['catid'] = $catid;
        $modelid = $categorys[$catid]['modelid'];
        $r = $m('add',$info,$modelid,'c_'.$common['MODEL'][$modelid]['tablename']);
        if(is_int($r)>0 || $r>0 ) {
            if($publish['siteid']>0){
                $publish['contentid'] = $r; 
                $rm = xt_load('model','cms.releasedmodel');
                $r = $rm('add',$publish);
            }
            return xtr_message('add_content_success',"/cms/content/$catid/$info[folder]");
        }
        return xtr_message($r); 
    }
    $catpath = '';
    foreach(explode(',',$categorys[$catid]['parentids']) as $id){
        if($id==0) continue;
        $catpath .='&nbsp;&raquo;&nbsp;'.$categorys[$id]['catname'];
    }
    xt_set('catpath',$catpath.'&nbsp;&raquo;&nbsp;'.$categorys[$catid]['catname']);
    $modelid = $categorys[$catid]['modelid'];
    if(!file_exists(CACHE_MODEL_PATH.'fields_'.$modelid.'.cache.php')){
        $m = xt_load('model','cms.fieldmodel');
        $m('cache',$modelid);
    }

    xt_load('cache','model/content_form',1);
    xt_load('class','form');
    $content_form = new content_form($modelid);
    xt_set('forminfos',$content_form->get());
    xt_set('catid',$catid);
    xt_set('modelid',$modelid);
    $sm = xt_load('model','cms.sitemodel');
    xt_set('cList',$sm('select',$common['SITE'], 0, 'publish[siteid]', 'siteid', 0,'请选择站点栏目','',2));
    return 'cms/addcontent';
}
/*public */function xtb_cms_editcontent($contentid = 0) {
    global $common;
    $m = xt_load('model','cms.contentmodel');
    $categorys  = $common['CATEGORY'];
    $data =  $m('get',$contentid);
    if(!$data) {
        return xtr_message('edit_content_fail');    
    }
    $catid = $data['catid'];
    if(!isset($categorys[$catid])){
        return xtr_message('edit_content_fail');
    }
    if (xt_pget('submit')) {
        $info = xt_pget('info',array());
        if(isset($info['inputtime'])) $info['updatetime'] = $info['inputtime'];
        $m = xt_load('model','cms.contentmodel');
        //$info['catid'] = $catid;
        $modelid = $categorys[$catid]['modelid'];
        $r = $m('edit',$contentid , $info , $modelid , 'c_'.$common['MODEL'][$modelid]['tablename']);
        if(is_int($r)>0 || $r>0 ) {
            return xtr_message('edit_content_success',"/cms/content/$catid/$info[folder]");
        }
        return xtr_message($r);
    }
    $catpath = '';
    foreach(explode(',',$categorys[$catid]['parentids']) as $id){
        if($id==0) continue;
        $catpath .='&nbsp;&raquo;&nbsp;'.$categorys[$id]['catname'];
    }
    xt_set('catpath',$catpath.'&nbsp;&raquo;&nbsp;'.$categorys[$catid]['catname']);

    $modelid = $categorys[$catid]['modelid'];
    if(!file_exists(CACHE_MODEL_PATH.'fields_'.$modelid.'.cache.php')){
        $m = xt_load('model','cms.fieldmodel');
        $m('cache',$modelid);
    }
    $mdata =  $m('getmodel', $contentid, 'c_'.$common['MODEL'][$modelid]['tablename']);
    if($mdata && is_array($mdata)){ $data = array_merge($data, $mdata); }
    xt_load('cache','model/content_form',1);
    xt_load('class','form');
    $content_form = new content_form($modelid);
    xt_set('forminfos',$content_form->get($data));
    xt_set('catid',$catid);
    xt_set('modelid',$modelid);
    xt_set('contentid',$contentid);
    return 'cms/editcontent';
}
/*public */function xtb_cms_model() {
    $m = xt_load('model','cms.modelmodel');
    xt_set('data',$m('select'));
    xt_set('tpre',xt_config('tablepre_icweb'));
    return 'cms/model';
}
/*public */function xtb_cms_actionmodel($type, $modelid) {
    switch($type){
        case '1': //disable model
            $m = xt_load('model','cms.modelmodel');
            $m('disable',$modelid);
            return xtr_message('disable_model_success','/cms/model');
        case '2': //disable field
            $m = xt_load('model','cms.modelmodel');
            $m('disable',$modelid);
            return xtr_message('disable_model_success','/cms/model');
        case '3': //preview
            if(!file_exists(CACHE_MODEL_PATH.'fields_'.$modelid.'.cache.php')){
                $m = xt_load('model','cms.fieldmodel');
                $m('cache',$modelid);    
            }
                
            xt_load('cache','model/content_form',1);
            xt_load('class','form');
			$content_form = new content_form($modelid);
			xt_set('forminfos',$content_form->get());
			xt_set('modelid',$modelid);
            return 'cms/previewmodel';    
    }
}
/*public */function xtb_cms_addmodel() {
    if (xt_pget('submit')) {
        $info = xt_pget('info',array());
        if(!is_array($info) || empty($info['name']) || empty($info['tablename'])) {
            return xtr_message('add_model_fail');
        }
        $m = xt_load('model','cms.modelmodel');
        $result = $m('add',$info);
        if($result<0){
            return xtr_message('add_model_fail');
        }
        return xtr_message('add_model_success',"/cms/editmodel/$result");
    }
    xt_set('tpre',xt_config('tablepre_icweb'));
    return 'cms/addmodel';
}

/*public */function xtb_cms_editmodel($modelid) {
    if (xt_pget('submit')) {
        $info = xt_pget('info',array());
        if(!is_array($info) || empty($info['name']) || empty($info['tablename'])) {
            return xtr_message('edit_addmodel_fail');
        }
        $m = xt_load('model','cms.modelmodel');
        $result = $m('edit',$modelid,$info);
        if($result<0){
            return xtr_message('edit_model_fail');
        }
        return xtr_message('edit_model_success',"/cms/editmodel/$modelid");
    }
    $m = xt_load('model','cms.modelmodel');
    xt_set('model',$m('get',$modelid));
    xt_set('modelid',$modelid);
    xt_set('tpre',xt_config('tablepre_icweb'));
    return 'cms/editmodel';
}
/*public */function xtb_cms_field($modelid) {
    $m = xt_load('model','cms.fieldmodel');
    xt_set('data',$m('select',$modelid));
    xt_set('modelid',$modelid);
    return 'cms/field';
}
/*public */function xtb_cms_actionfield($type, $modelid, $fieldid) {
    global $common;
    switch($type){
        case '1': //disable field
            $m = xt_load('model','cms.fieldmodel');
            $m('disable',$modelid,$fieldid);
            return xtr_message('disable_field_success','/cms/field/'.$modelid);
       case '2': //remove field
            $m = xt_load('model','cms.fieldmodel');
            $m('delete',$modelid, $fieldid,'c_'.$common['MODEL'][$modelid]['tablename']);
            return xtr_message('delete_field_success','/cms/field/'.$modelid);
    }
}
/*public */function xtb_cms_addfield($modelid) {
    global $common;
    if (xt_pget('submit')) {
        $info = xt_pget('info',array());
        $info['modelid'] = $modelid;
        $m = xt_load('model','cms.fieldmodel');
        $info['unsetgroupids'] = '';
        $info['unsetroleids']  = '';
        if(!is_array($info) || empty($info['field']) || empty($info['name'])
            || $info['minlength']=='' || $info['maxlength']=='' 
            || !preg_match("/^[a-z][0-9a-z_]*[0-9a-z]?$/i", $info['field'])){
            return xtr_message('add_field_fail');
        }
        $result = $m('add',$modelid, $info, xt_pget('setting',array()),'c_'.$common['MODEL'][$modelid]['tablename']);
        if($result<0){
            return xtr_message('add_field_fail');   
        }
        return xtr_message('add_field_success',"/cms/editfield/$modelid/$result");
    }
    xt_set('modelid',$modelid);
    require_once MS_FIELDS_PATH.'patterns.inc.php';
    xt_set('patterns',$patterns);
    return 'cms/addfield';
}
/*public */function xtb_cms_editfield($modelid, $fieldid) {
    global $common;
    if (xt_pget('submit')) {
        $info = xt_pget('info',array());
        if(!is_array($info) || empty($info['field']) || empty($info['name'])
            || $info['minlength']=='' || $info['maxlength']=='' || xt_pget('formtype')==''
            || !preg_match("/^[a-z][0-9a-z_]*[0-9a-z]?$/i", $info['field'])){
            return xtr_message('edit_field_fail');
        }
        $info['modelid'] = $modelid;
        $m = xt_load('model','cms.fieldmodel');
        if($m('edit',$modelid, $fieldid, $info, xt_pget('setting',array()),
            xt_pget('formtype'),xt_pget('issystem'),'c_'.$common['MODEL'][$modelid]['tablename']) >= 0);
            return xtr_message('edit_field_success',"/cms/editfield/$modelid/$fieldid");
        return xtr_message('edit_field_fail');
    }
    $m = xt_load('model','cms.fieldmodel');
    xt_set('m',$m('get',$fieldid));
    xt_set('modelid',$modelid);
    xt_set('fieldid',$fieldid);
    require_once MS_FIELDS_PATH.'patterns.inc.php';
    xt_set('patterns',$patterns);
    return 'cms/editfield';
}
/*public */function xtb_cms_publish($contentid=0,$siteid=0) {
    global $common;
    $sites  = $common['SITE'];
    $sitepath= '';
    if(isset($sites[$siteid])) {
        foreach(explode(',',$sites[$siteid]['parentids']) as $id){
            if($id==0) continue;
            $sitepath .='&nbsp;&raquo;&nbsp;'.$sites[$id]['sitename'];
        }
        xt_set('sitepath',$sitepath.'&nbsp;&raquo;&nbsp;'.$sites[$siteid]['sitename']);
    }
    $sm = xt_load('model','cms.sitemodel');
    xt_set('cList',$sm('select',$common['SITE'],0, 'info[siteid]', 'siteid',$siteid,'', '',0,1));
    xt_set('siteid',$siteid);
    xt_set('contentid',$contentid);
    return 'cms/publish';
}
/*public */function xtb_cms_editpublish($contentid=0,$siteid=0) {
    global $common;
    $sites  = $common['SITE'];
    $sitepath= '';
    if(isset($sites[$siteid])) {
        foreach(explode(',',$sites[$siteid]['parentids']) as $id){
            if($id==0) continue;
            $sitepath .='&nbsp;&raquo;&nbsp;'.$sites[$id]['sitename'];
        }
        xt_set('sitepath',$sitepath.'&nbsp;&raquo;&nbsp;'.$sites[$siteid]['sitename']);
    }
    $sm = xt_load('model','cms.sitemodel');
    xt_set('cList',$sm('select',$common['SITE'],0, 'info[siteid]', 'siteid',$siteid,'', '',0,1));
    xt_set('siteid',$siteid);
    xt_set('contentid',$contentid);
    return 'cms/editpublish';
}

/*public */function xtb_cms_released($siteid=0) {
    global $common;
    $catid = xt_gget('catid',0);
    $sites  = $common['SITE'];
    xt_set('sites',$sites);
    xt_set('categorys',$common['CATEGORY']);
    $sitepath= '';
    if(isset($sites[$siteid])) {
        foreach(explode(',',$sites[$siteid]['parentids']) as $id){
            if($id==0) continue;
            $sitepath .='&nbsp;&raquo;&nbsp;'.$sites[$id]['sitename'];
        }
        xt_set('sitepath',$sitepath.'&nbsp;&raquo;&nbsp;'.$sites[$siteid]['sitename']);
    }
    $cm = xt_load('model','cms.categorymodel');
    xt_set('cList',$cm('select',$common['CATEGORY'],0, 'catid', 'catid', '', $catid,'',2));
    $rm = xt_load('model','cms.releasedmodel');
    $perpage = intval(xt_gget('perpage',20));
    $page = intval(xt_gget('page',1));
    $pageSpport = $rm('select',$siteid,$catid, $page, $perpage, xt_gget('field'),xt_gget('q'));
    xt_set('data',xt_hashmap($pageSpport,'data'));
    xt_set('pagectrl',xt_hashmap($pageSpport,'pagectrl'));
    xt_set('pageurl',$pageSpport['pageurl'].'&perpage='.$perpage);
    
    xt_set('siteid',$siteid);
    return 'cms/released';
}
/*public */function xtb_cms_getcode($siteid = 0) {
    global $common;
    $sm = xt_load('model','cms.sitemodel');
    xt_set('cList',$sm('select',$common['SITE'],0, 'siteid', 'siteid', $siteid, '', '', 0 ,1));
    xt_set('siteid',$siteid);
    return 'cms/getcode';
}
/*public */function xtb_cms_setaddfield() {
    $formtype = xt_gget('formtype');
    if(!$formtype) return NONE;
    require_once MS_FIELDS_PATH.'patterns.inc.php';
    require_once MS_FIELDS_PATH.$formtype.'/field_add_form.inc.php';
    return NONE;
}
/*public */function xtb_cms_seteditfield($fieldid) {
    $formtype = xt_gget('formtype');
    if(!$formtype) return NONE;
    $m = xt_load('model','cms.fieldmodel');
    $f = $m('get',$fieldid);
    eval("\$setting = $f[setting];");
    @extract($setting);
    require_once MS_FIELDS_PATH.'patterns.inc.php';
    require_once MS_FIELDS_PATH.$formtype.'/field_edit_form.inc.php';
    return NONE;
}
/*public */function xtb_cms_cache() {
    $cachemodel = xt_load('model','cms.cachemodel');
    $cachemodel('reflashCommon');
    $m = xt_load('model','cms.modelmodel');
    $m('cache');
    return xtr_message('cache_success');
}
?>